package ynu.edu.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ynu.edu.entities.Address;

import java.util.List;

@Repository
public interface AddressRepository extends JpaRepository<Address, Integer> {
    
    /**
     * 根据用户ID查询送货地址列表
     */
    List<Address> findByUserIdOrderByIsDefaultDesc(String userId);
    
    /**
     * 根据地址ID和用户ID查询送货地址
     */
    Address findByDaIdAndUserId(Integer daId, String userId);
    
    /**
     * 将用户的所有地址设置为非默认
     */
    @Modifying
    @Query("UPDATE Address a SET a.isDefault = 0 WHERE a.userId = :userId")
    void clearDefaultAddress(@Param("userId") String userId);
} 